package DAL;

import Entity.BCategory;
import Entity.Books;
import Entity.Category;
import IDAL.BookTypePort;
import Utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.util.List;

/**
 * @Author Chen
 * @Date 2022/1/7 14:33
 * @Version 1.0
 */
public class BooksTypeDao implements BookTypePort {


    @Override
    public int addBookType(Object... args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "insert into l_bCategory(bookTypeName)values(?)";

            result = runner.update(conn, sql, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public int updateBookType(Object... args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "update l_bCategory set " +
                    " bookTypeName = ?" +
                    " where bookTypeNumber = ?";
            result = runner.update(conn, sql, args);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public int deleteBookType(Object... args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "delete from l_bCategory where bookTypeNumber=?";
            result = runner.update(conn, sql, args);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public List<BCategory> selectBookType(Object... args) {
        List<BCategory> result = null;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "select * from l_bCategory";
            BeanListHandler<BCategory> handler = new BeanListHandler<>(BCategory.class);
            result = runner.query(conn, sql, handler);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public BCategory selectBookTypeName(Object... args) {
        BCategory result=null;
        Connection conn=null;
        try {
            QueryRunner runner=new QueryRunner();
            conn=JDBCUtils.getConnection3();
            String sql="select * from l_bCategory where bookTypeName=?";
            BeanHandler<BCategory> bh=new BeanHandler<>(BCategory.class);
            result=runner.query(conn,sql,bh,args);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
        return result;
    }

    //根据图书类型编号查找！
    public BCategory selectBookTypeId(Object... args){
        BCategory result=null;
        Connection conn=null;
        try {
            QueryRunner runner=new QueryRunner();
            conn=JDBCUtils.getConnection3();
            String sql="select * from l_bCategory where bookTypeNumber=?";
            BeanHandler<BCategory> bh=new BeanHandler<>(BCategory.class);
            result=runner.query(conn,sql,bh,args);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
        return result;
    }
}
